package com.ihighest.app.infox.adapter;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.bean.ImageItem;
import com.ihighest.app.infox.tools.NativeImageLoader;
import com.ihighest.app.infox.tools.NativeImageLoader.NativeImageCallBack;

public class MyGridAdapter extends BaseAdapter {

	private List<ImageItem> list;
	private LayoutInflater inflater;
	private GridView mGridView;
	private Point mPoint;// ������װImageView�Ŀ�͸ߵĶ���
	private Handler handler = new Handler();

	private ExecutorService executor = Executors.newFixedThreadPool(10);

	public MyGridAdapter(Context context, List<ImageItem> list, GridView mGridView, int width) {
		this.list = list;
		this.mPoint = new Point(width, width);
		inflater = LayoutInflater.from(context);
		this.mGridView = mGridView;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder;
		if (convertView == null) {
			mViewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_image_item, null);
			mViewHolder.mImageView = (ImageView) convertView.findViewById(R.id.image_item_image);
			mViewHolder.tiImageview = (ImageView) convertView.findViewById(R.id.image_item_tishi);
			convertView.setTag(mViewHolder);

		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}

		String path = list.get(position).path;
		mViewHolder.mImageView.setTag(path);
		mViewHolder.tiImageview.setSelected(list.get(position).type);
		;
		// mViewHolder.mImageView.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if(mViewHolder.tiImageview.isSelected()){
		// mViewHolder.tiImageview.setSelected(false);
		// }else{
		// mViewHolder.tiImageview.setSelected(true);
		// }
		//
		// }
		// });
		Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(path, mPoint, new NativeImageCallBack() {

			@Override
			public void onImageLoader(Bitmap bitmap, String path) {
				ImageView mImageView = (ImageView) mGridView.findViewWithTag(path);
				if (bitmap != null && mImageView != null) {
					mImageView.setImageBitmap(bitmap);
				}
			}
		});

		if (bitmap != null) {
			mViewHolder.mImageView.setImageBitmap(bitmap);
		} else {

			mViewHolder.mImageView.setImageResource(R.drawable.infox_img_loadrong);
		}

		return convertView;
	}

	private void asyncImageLoad(final ImageView mImageView, final String path) {
		executor.submit(new Runnable() {

			@Override
			public void run() {
				final Drawable drawable = Drawable.createFromPath(path);
				handler.post(new Runnable() {

					@Override
					public void run() {
						mImageView.setImageDrawable(drawable);

					}
				});

			}
		});
	}

	public static class ViewHolder {
		public ImageView mImageView;
		public ImageView tiImageview;
	}

}
